package ext.tianma.part.processors;

import java.io.Serializable;
import java.util.List;

import com.ptc.core.components.beans.ObjectBean;
import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.com.iba.IBAUtil;
import ext.tianma.workflow.SubmitBomReview;
import wt.fc.QueryResult;
import wt.method.RemoteAccess;
import wt.part.WTPart;
import wt.util.WTException;
import wt.workflow.engine.WfEngineHelper;
import wt.workflow.engine.WfProcess;
import wt.workflow.engine.WfState;

/**
 * 用于启动二级BOM变更
 * @author laichao_sun
 *
 */
public class SecondBOMchange extends DefaultObjectFormProcessor implements RemoteAccess, Serializable
{
	@Override
	public FormResult doOperation(NmCommandBean arg0, List<ObjectBean> arg1) throws WTException {
		FormResult formresult = new FormResult();
		formresult.setStatus(FormProcessingStatus.SUCCESS);
	
		NmOid nmoid = arg0.getActionOid();
		WTPart currentPart = (WTPart) nmoid.getRefObject();	
	
		if( currentPart != null ){
			boolean flag = false;
			QueryResult qr = WfEngineHelper.service.getAssociatedProcesses(currentPart, WfState.OPEN_RUNNING, null);
			WfProcess process = null;
			while (qr.hasMoreElements()) {
				process = (WfProcess) qr.nextElement();
				if (process.getTemplate().getName().equals("天马二级BOM变更流程")) {
					flag = true;
				}
			}
			if(!flag) {
				//启动流程清空 Sourcing二级物料BOM信息属性
				IBAUtil.forceSetIBAValue(currentPart, "SourcingUpdateReason", "");
				SubmitBomReview.submitWorkflow( currentPart ,"天马二级BOM变更流程",false);
				FeedbackMessage feedbackmessage = new FeedbackMessage(
						FeedbackType.SUCCESS, null, "启动天马二级BOM变更流程", null, new String[] {});
				formresult.addFeedbackMessage(feedbackmessage);
				return formresult;
			}
		}
	
		FeedbackMessage feedbackmessage = new FeedbackMessage(
				FeedbackType.FAILURE, null, "启动天马二级BOM变更流程", null,"该流程已存在,请查看!");
		formresult.addFeedbackMessage(feedbackmessage);
		return formresult;
	}
}
